package com.openlogic.common.rabbitmq.config.properties;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Singleton;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: RabbitProperties
 * @Description: rabbitMQ自定义配置
 * @author weiyongpeng
 * @date 2025年1月21日 下午8:13:00
 */
@Singleton(value = true)
@Configuration
@Inject(value = "${openlogic.mq.rabbit}",autoRefreshed = true)
public class RabbitProperties {

	/** 地址（默认） */
	private String host;

	/** 端口（默认） */
	private Integer port;

	/** 账号（默认） */
	private String username;

	/** 密码（默认） */
	private String password;

	/** 前缀标识 */
	private String prefix = StrUtil.EMPTY;

	/** 环境标识 */
	private String evn = StrUtil.EMPTY;

	/** 交换机配置 */
	private List<ExchangeInfo> exchangeInfos;

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getEvn() {
		return evn;
	}

	public void setEvn(String evn) {
		this.evn = evn;
	}

	public List<ExchangeInfo> getExchangeInfos() {
		return exchangeInfos;
	}

	public void setExchangeInfos(List<ExchangeInfo> exchangeInfos) {
		this.exchangeInfos = exchangeInfos;
	}

	/**
	 * 交换机配置
	 */
	public static class ExchangeInfo {

		/** 地址（自定义） */
		private String host;

		/** 端口（自定义） */
		private Integer port;

		/** 账号（自定义） */
		private String username;

		/** 密码（自定义） */
		private String password;

		/**
		 * 交换机类型（direct直连交换机 topic主题交换机 fanout扇出交换机 headers头交换机
		 * x-delayed-message延时消息交换机）
		 */
		private String type;

		/** 交换机名称 */
		private String name;

		/** 是否持久化 */
		private Boolean durable = Boolean.TRUE;

		/** 该队列是否只能被当前连接使用，连接关闭则队列删除。 优先级高于durable */
		private Boolean exclusive = Boolean.FALSE;

		/** 是否自动删除队列（没有生产者或消费者使用此队列，则此队列自动删除） */
		private Boolean autoDelete = Boolean.FALSE;

		/** 自定义参数 */
		private Map<String, Object> params;

		/** 队列配置 */
		private List<QueueInfo> queueInfos;

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public Integer getPort() {
			return port;
		}

		public void setPort(Integer port) {
			this.port = port;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Boolean getDurable() {
			return durable;
		}

		public void setDurable(Boolean durable) {
			this.durable = durable;
		}

		public Boolean getExclusive() {
			return exclusive;
		}

		public void setExclusive(Boolean exclusive) {
			this.exclusive = exclusive;
		}

		public Boolean getAutoDelete() {
			return autoDelete;
		}

		public void setAutoDelete(Boolean autoDelete) {
			this.autoDelete = autoDelete;
		}

		public Map<String, Object> getParams() {
			return MapUtils.isEmpty(params) ? new HashMap<>() : params;
		}

		public void setParams(Map<String, Object> params) {
			this.params = params;
		}

		public List<QueueInfo> getQueueInfos() {
			return queueInfos;
		}

		public void setQueueInfos(List<QueueInfo> queueInfos) {
			this.queueInfos = queueInfos;
		}

		/**
		 * 队列配置
		 */
		public static class QueueInfo {

			/** 路由键 */
			private String routingKey;

			/** 队列名称 */
			private String name;

			/** 是否持久化 */
			private Boolean durable = Boolean.TRUE;

			/** 该队列是否只能被当前连接使用，连接关闭则队列删除。 优先级高于durable */
			private Boolean exclusive = Boolean.FALSE;

			/** 是否自动删除队列（没有生产者或消费者使用此队列，则此队列自动删除） */
			private Boolean autoDelete = Boolean.FALSE;

			/** 自定义参数 */
			private Map<String, Object> params;

			public String getRoutingKey() {
				return routingKey;
			}

			public void setRoutingKey(String routingKey) {
				this.routingKey = routingKey;
			}

			public String getName() {
				return name;
			}

			public void setName(String name) {
				this.name = name;
			}

			public Boolean getDurable() {
				return durable;
			}

			public void setDurable(Boolean durable) {
				this.durable = durable;
			}

			public Boolean getExclusive() {
				return exclusive;
			}

			public void setExclusive(Boolean exclusive) {
				this.exclusive = exclusive;
			}

			public Boolean getAutoDelete() {
				return autoDelete;
			}

			public void setAutoDelete(Boolean autoDelete) {
				this.autoDelete = autoDelete;
			}

			public Map<String, Object> getParams() {
				return params;
			}

			public void setParams(Map<String, Object> params) {
				this.params = params;
			}

			@Override
			public String toString() {
				return "QueueInfo [routingKey=" + routingKey + ", name=" + name + ", durable=" + durable
						+ ", exclusive=" + exclusive + ", autoDelete=" + autoDelete + ", params=" + params + "]";
			}

		}

		@Override
		public String toString() {
			return "ExchangeInfo [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
					+ ", type=" + type + ", name=" + name + ", durable=" + durable + ", exclusive=" + exclusive
					+ ", autoDelete=" + autoDelete + ", params=" + params + ", queueInfos=" + queueInfos + "]";
		}

	}

	/**
	 * 描述：获取系统环境前缀
	 * @Title: getEvnPrefix
	 * @return
	 * @author weiyongpeng
	 * @date 2025年1月21日 下午8:45:55
	 */
	public String getEvnPrefix() {
		String prefix = StrUtil.isNotBlank(this.prefix) ? StrUtil.format("{}.", this.prefix) : StrUtil.EMPTY;
		String evn = StrUtil.isNotBlank(this.evn) ? StrUtil.format("{}.", this.evn) : StrUtil.EMPTY;
		return StrUtil.format("{}{}", prefix, evn);
	}

	@Override
	public String toString() {
		return "RabbitProperties [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
				+ ", prefix=" + prefix + ", evn=" + evn + ", exchangeInfos=" + exchangeInfos + "]";
	}

}
